import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ost_flutter_module/class/store/pay_page/pay_amount.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/common/dynamic_cell/dynamic_cell_model.dart';
import 'package:ost_flutter_module/common/dynamic_cell/widgets/dynamic_cell_listtileinfo.dart';
import 'package:ost_flutter_module/common/dynamic_cell/widgets/dynamic_cell_mutlisttileinfo.dart';
import 'package:ost_flutter_module/common/dynamic_cell/widgets/dynamic_cell_shopinfo.dart';
import 'package:ost_flutter_module/common/dynamic_cell/widgets/dynamic_cell_userinfo.dart';
import 'package:ost_flutter_module/common/styles.dart';
import 'package:ost_flutter_module/common/utils.dart';
import 'package:ost_flutter_module/help/ost_route.dart';
import 'package:ost_flutter_module/widget/dialog/show_bottom_sheet.dart';

import 'bloc/order_page_bloc.dart';

// ignore: must_be_immutable
class StoreOrderPageBody extends StatelessWidget {
  final String info;
  StoreOrderPageBody({Key key, this.info}) : super(key: key);

  // 定义基础数据
  List<DynamicCellModel> _list = [
    DynamicCellModel(type: DynamicCellType.userInfo),
    DynamicCellModel(type: DynamicCellType.shopInfo),
    DynamicCellModel(type: DynamicCellType.shopInfo),
    DynamicCellModel(
        type: DynamicCellType.listTileInfo, icon: 'icon_discount_red'),
    DynamicCellModel(type: DynamicCellType.mutListTileInfo),
  ];

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<OrderPageBloc, OrderPageState>(
      builder: (context, state) {
        return Stack(children: [
          ListView.builder(
            itemCount: 5,
            itemBuilder: (BuildContext context, int index) {
              DynamicCellModel model = _list[index];
              DynamicCellType type = model.type;

              if (type == DynamicCellType.userInfo) {
                return DynamicCellUserInfo();
              } else if (type == DynamicCellType.shopInfo) {
                return DynamicCellShopInfo();
              } else if (type == DynamicCellType.listTileInfo) {
                return DynamicCellListTileInfo(
                  text: '整单优惠',
                  leading: Utils.svgAssets('icon_order_discount_red'),
                  trailText: '-￥150',
                );
              } else if (type == DynamicCellType.mutListTileInfo) {
                return DynamicCellMulListTileInfo(
                  list: ['课程优惠券x1', '使用电子钱包支付'],
                  svgs: [
                    Utils.svgAssets('icon_order_discount_yellow'),
                    Utils.svgAssets('icon_order_wallet'),
                  ],
                  shadow: true,
                  onTap: (data, index) {
                    if (index == 0)
                      // 优惠券
                      Navigator.pushNamed(context, GRouter.discount_page);
                    else
                      // 支付
                      gShowBottomSheet(
                        context,
                        child: StorePayAmountWidget(),
                        cancel: () {},
                        done: (value) {
                          gShowBottomSheet(
                            context,
                            child: StorePayModeWidget(),
                            cancel: () {},
                            text: '确认付款',
                            showDone: false,
                          );
                        },
                      );
                  },
                );
              }
              return Container();
            },
          ),
          Positioned(left: 0, right: 0, bottom: 0, child: StoreOrderProtocol()),
        ]);
      },
    );
  }
}

class StoreOrderProtocol extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: colorE2F1FF,
      height: Adapt.px(36),
      child: Padding(
        padding: EdgeInsets.symmetric(
            horizontal: Adapt.px(16), vertical: Adapt.px(10)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.check_circle, color: color2A51A1, size: 16),
            SizedBox(width: Adapt.px(8)),
            Expanded(
              child: RichText(
                text: TextSpan(
                    text: '我已阅读并同意东方启音',
                    style: TextStyles.detailTitleStyle,
                    children: [
                      TextSpan(
                        text: '《培训服务协议》',
                        style: TextStyle(
                          decoration: TextDecoration.underline,
                          color: color2A51A1,
                        ),
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {
                            Utils.showSnackBar(context, '点击了协议');
                          },
                      )
                    ]),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
